package org.mcflier.appuntamento.tests.main;

import java.util.List;
import java.util.Set;

import org.mcflier.appuntamento.core.Factory;
import org.mcflier.appuntamento.core.exception.ModulesException;
import org.mcflier.appuntamento.core.modules.ModulesManager;
import org.mcflier.appuntamento.data.model.TorrentList;
import org.mcflier.appuntamento.io.driver.db.JDBCDriver;
import org.mcflier.appuntamento.io.driver.rss.torrent.AbstractRssTorrentDriver;
import org.mcflier.appuntamento.io.driver.rss.torrent.TorrentAggregator;
import org.mcflier.appuntamento.io.exception.RSSConnectionException;
import org.mcflier.appuntamento.io.exception.RSSParsingException;

public class Test {

	public static void main(String[] args) {
		Factory factory = Factory.getInstance();
		try {
			ModulesManager modules = (ModulesManager)factory.getModuleInstance(Factory.MODULE_MODULES);
			modules.loadModules();
			TorrentAggregator torrents = (TorrentAggregator)factory.getModuleInstance(Factory.MODULE_RSS_TORRENT);
			JDBCDriver jdbc = (JDBCDriver)factory.getModuleInstance(Factory.MODULE_JDBC);
			
			Set<String> keys = torrents.getDrivers().keySet();
			AbstractRssTorrentDriver driver;
			for (String key : keys) {
				driver = torrents.getDriver(key);
				System.out.println("Torrents from driver '"+driver.getSource().getName()+"'");
				
				System.out.println("HD");
				List<TorrentList> list = driver.getHDTorrents();
				int hdTorrents = (list.size()!=0)?list.get(0).getItems().size():0;
				int hdTorrentsDB = 0;
				for (TorrentList torrentList : list) {
					hdTorrentsDB += jdbc.addTorrents(torrentList);
				}
				
				System.out.println("SD");
				list = driver.getSDTorrents();
				int sdTorrents = (list.size()!=0)?list.get(0).getItems().size():0;
				int sdTorrentsDB = 0;
				for (TorrentList torrentList : list) {
					sdTorrentsDB += jdbc.addTorrents(torrentList);
				}
				
				System.out.println("Undefined");
				list = driver.getUndefinedTorrents();
				int undefinedTorrents = (list.size()!=0)?list.get(0).getItems().size():0;
				int undefinedTorrentsDB = 0;
				for (TorrentList torrentList : list) {
					undefinedTorrents += jdbc.addTorrents(torrentList);
				}
				
				System.out.println("Leidos:");
				System.out.println("Torrents HD: "+hdTorrents);
				System.out.println("Torrents SD: "+sdTorrents);
				System.out.println("Torrents Undefined: "+undefinedTorrents);
				System.out.println("Guardados:");
				System.out.println("Torrents HD: "+hdTorrentsDB);
				System.out.println("Torrents SD: "+sdTorrentsDB);
				System.out.println("Torrents undefined: "+undefinedTorrentsDB);
			}
		} catch (RSSConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RSSParsingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ModulesException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
}